import { createRouter, createWebHistory } from 'vue-router'
import index from '../view/user/index.vue'
// 登录相关页面
import login from '../view/login.vue'
import register from '../view/register.vue'
import forgotPassword from '../view/forgotPassword.vue'
// 用户端地图页面
import UserMap from "@/view/map/UserMap.vue";
import MapContainer from "@/view/map/MapContainer.vue";
import { ElMessage } from 'element-plus'
//用户端 秒杀界面
import Eleven from "@/view/user/seckill/eleven.vue";
import SeckillOrderProfile from "@/view/user/seckill/seckillOrderProfile.vue";
//用户端页面
import profile from "@/view/user/profile.vue";
import Shopping from "@/view/user/shopping.vue";
import GoodsList from "@/view/user/goodsList.vue";
import GoodsProfile from "@/view/user/goodsProfile.vue";
import OrderProfile from "@/view/user/orderProfile.vue";
import OrderList from "@/view/user/orderList.vue";
import Payment from "@/view/user/payment.vue";



// 管理员端页面
import AdminIndex from "@/view/admin/adminIndex.vue";
import AdminList from "@/view/admin/adminList.vue";
import UserList from "@/view/admin/userList.vue";
import PaymentList from "@/view/admin/paymentList.vue";
import Goods from "@/view/admin/goods.vue";
import Order from "@/view/admin/order.vue";
import OrderAddr from "@/view/admin/orderAddr.vue";
import DeliveryAddr from "@/view/admin/deliveryAddr.vue";
// 管理员 秒杀信息
import SeckillGoods from "@/view/admin/seckill/seckillGoods.vue";
import SeckillActivity from "@/view/admin/seckill/seckillActivity.vue";
import SeckillOrder from "@/view/admin/seckill/seckillOrder.vue";
import SeckillResult from "@/view/admin/seckill/seckillResult.vue";




const routes = [
    {
        path: '/login',
        name: 'login',
        component: login
    },
    {
        path: '/admin-login',
        name: 'adminLogin',
        component: login
    },
    {
        path: '/register',
        name: 'register',
        component: register
    },
    {
        path: '/forgot-password',
        name: 'forgotPassword',
        component: forgotPassword
    },
    {
        path:'/',
        name:'index',
        component:index,
        children:[
            {path:'/user/profile',component:profile},
            {path:'/map/UserMap',component:UserMap},
            {path:'/user/shopping',component: Shopping, name: 'shopping' },
            {path:'/user/goods-list',component: GoodsList, name: 'goodsList' },
            {path:'/user/goods-profile/:id',component: GoodsProfile, name: 'goodsProfile', props: true},
            {path:'/user/order-profile/:id',component: OrderProfile, name: 'orderProfile', props: true},
            {path: '/user/orderList',component: OrderList},
            {path: '/user/payment',component: Payment},
            {path: '/user/seckill/eleven',component: Eleven},
            {path: '/user/seckill/seckillOrderProfile/:id',component: SeckillOrderProfile},
        ]
    },
    {
        path:'/admin/adminIndex',
        name:'adminIndex',
        component:AdminIndex,
        children:[
            {path:'/admin/adminList',component:AdminList},
            {path:'/admin/userList',component:UserList},
            {path:'/admin/deliveryAddr',component:DeliveryAddr},
            {path:'/admin/goods',component:Goods},
            {path:'/admin/order',component:Order},
            {path:'/admin/orderAddr',component:OrderAddr},
            {path:'/admin/paymentList',component: PaymentList},
            {path:'/admin/seckill/seckillGoods',component: SeckillGoods},
            {path:'/admin/seckill/seckillActivity',component: SeckillActivity},
            {path:'/admin/seckill/seckillOrder',component: SeckillOrder},
            {path:'/admin/seckill/seckillResult',component: SeckillResult},
        ]
    },

]

const router = createRouter({
    history: createWebHistory(),
    routes
})


// 添加全局路由守卫
router.beforeEach((to, from, next) => {
    // 获取token
    const token = localStorage.getItem('token');

    // 定义不需要登录的页面
    const whiteList = ['/login', '/admin-login', '/register', '/forgot-password'];

    // 如果访问的是不需要登录的页面，直接放行
    if (whiteList.includes(to.path)) {
        next();
        return;
    }

    // 如果没有token，重定向到登录页
    if (!token) {
        ElMessage.error('请先登录');
        next('/login');
        return;
    }

    // 解析token获取用户角色信息
    try {
        // 假设token是JWT格式，可以通过解析获取payload
        const payload = JSON.parse(atob(token.split('.')[1]));
        console.log(payload);
        const role = payload.ROLE;
        console.log(role);

        // 根据角色控制访问权限
        if (to.path.startsWith('/user/') || to.path === '/' || to.path.startsWith('/map/')) {
            // 用户相关页面
            if (role === 'user') {
                // 用户和管理员都可以访问用户页面
                next();
            } else {
                ElMessage.error('权限不足');
                next('/login');
            }
        } else if (to.path.startsWith('/admin/')) {
            // 管理员相关页面
            if (role === 'admin') {
                // 只有管理员可以访问管理员页面
                next();
            } else {
                ElMessage.error('权限不足');
                next('/login');
            }
        } else {
            // 其他页面（如根路径）直接放行
            next();
        }
    } catch (error) {
        // token解析失败，重定向到登录页
        ElMessage.error('身份验证失败，请重新登录');
        next('/login');
    }
});


export default router;
